package naive2;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import entities.Entity;

import tools.LuceneIndexReader;

//retrieve json file based on entity's name
public class LuceneJsonRetriever {
	
	private LuceneIndexReader reader;
	
	//dir is the directory for index
	public LuceneJsonRetriever(String dir){
		
		this.reader = new LuceneIndexReader(dir);
		
	}
	public JSONObject getJSONObj(String name) throws JSONException{
		String jsonDoc = this.reader.queryEntityJSONIndex(name);
		if( (jsonDoc==null) || ( jsonDoc.equals(""))){
			return null;
		}else{
			return new JSONObject(jsonDoc);
		}
	}
	public LuceneIndexReader getReader(){
		return this.reader;
	}
	
	public static void main(String[] args){
		
		LuceneJsonRetriever lj = new LuceneJsonRetriever("/largedata1/cmput696/students/lyao1/lemmaIndex");
		try {
			//use mj23 cannot retrieve any content
			JSONObject jobj = lj.getJSONObj("Yeoseodo");
			/*
			System.out.println("keys:");
			for( Object key: jobj.keySet()){
				String skey =(String)key;
				System.out.println();
			}
			
			String name = jobj.getString("name");
			System.out.println("name:"+name);
			
			
			JSONObject keyobj = jobj.getJSONObject("keys");
			System.out.println("keys:");
			for(Object key: keyobj.keySet() ){
				System.out.println("\t"+keyobj.getString((String)key));
			}
			
			JSONObject conobj = jobj.getJSONObject("contexts");
			System.out.println("contexts");
			for(Object key: conobj.keySet() ){
				System.out.println("\t"+(String)key+" freq:"+conobj.getInt((String)key));
			}
			
			
			JSONObject catobj = jobj.getJSONObject("categories");
			System.out.println("categories");
			for(Object key: catobj.keySet() ){
				System.out.println("\t"+(String)key+" freq:"+catobj.getInt((String)key));
			}
			
			String al1 = jobj.getString("alternativeName1");
			System.out.println("al1:");
			System.out.println(al1);
			
			String al2 = jobj.getString("alternativeName2");
			System.out.println("al2:");
			if(( al2!=null)&&(!al2.equals(""))){
				System.out.println(al2);
			}
			
			JSONArray arr = jobj.getJSONArray("alternativeNames");
			System.out.println("als:");
			if( (arr!=null)&&(arr.length()>0) ){
				for (int i=0;i<arr.length();i++){
					System.out.println("\t"+arr.getString(i));
				}
			}
			
			
			JSONArray tarr = jobj.getJSONArray("types");
			System.out.println("types:");
			if( (tarr!=null)&&(tarr.length()>0) ){
				for (int i=0;i<tarr.length();i++){
					System.out.println("\t"+tarr.getString(i));
				}
			}
			
			JSONObject aobj = jobj.getJSONObject("aliases");
			System.out.println("aliases");
			for(Object key: aobj.keySet() ){
				System.out.println("\t"+(String)key+" freq:"+aobj.getJSONObject((String)key).getInt("frequency"));
			}*/
			
			JsonEntity2BoW j2bw = new JsonEntity2BoW(null);
			Entity entity = new Entity("Yeoseodo");
			JsonEntity je = JsonEntity.instance(entity, lj);
			BoW bow = j2bw.jentity2bow(je);
			System.out.println(bow);
			/*
			System.out.println("keys:");
			System.out.println(j2bw.getKeysString(jobj));
			System.out.println("contexts:");
			System.out.println(j2bw.getContextString(jobj));
			System.out.println("types:");
			System.out.println(j2bw.getTypesString(jobj));
			System.out.println("alias:");
			System.out.println(j2bw.getAliasString(jobj));
			System.out.println("alternativeNames:");
			System.out.println(j2bw.getAlternativeNames(jobj));
			System.out.println("name:");
			System.out.println(j2bw.getNameString(jobj));
			System.out.println("cateoreis");
			System.out.println(j2bw.getCategoriesString(jobj));*/
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
